package com.zjw.zy.coreui.fragment.access

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.constant.RegisterUserResult
import com.zjw.zy.ext.shortToast
import com.zjw.zy.viewmodel.AccessViewModel
import com.zjw.zy.viewmodel.WsMessageDispatchViewModel

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/12/7 14:13
 */
class AccessIdFragment : BaseFragment() {
    private val mAccessViewModel: AccessViewModel by viewModels({ requireParentFragment() })

    private val name: TextView by lazy { findViewById(R.id.access_it_name) }
    private val address: TextView by lazy { findViewById(R.id.access_it_address) }
    private val number: TextView by lazy { findViewById(R.id.access_it_number) }
    private val next: Button by lazy { findViewById(R.id.access_id_next) }
    private val last: Button by lazy { findViewById(R.id.access_id_last) }

    var tUsername = ""
    var tAddress = ""
    var tName = ""

    override fun onPageEnter() {

    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {
        mAccessViewModel.getIdInfo().observe(this) {
            name.text = resources.getString(R.string.access_id_name, it.name)
            address.text = resources.getString(R.string.access_id_address, it.address)
            number.text = resources.getString(R.string.access_id_number, it.username)

            //保存值
            tUsername = it.username
            tAddress = it.address
            tName = it.name
        }

        mAccessViewModel.getRegisterResult().observe(this) {
            when (it) {
                RegisterUserResult.SUCCESS -> {
                    "验证通过".shortToast()
                    (requireParentFragment() as AccessFragment).switchPage(AccessFragment.SUB_BIND_INDEX)
                }
                RegisterUserResult.FAILED -> {
                    "验证失败,请重新验证".shortToast()
                }
                RegisterUserResult.REGISTERED -> {
                    "一生只能注册一次哦".shortToast()
                    (requireParentFragment() as AccessFragment).switchPage(AccessFragment.SUB_BIND_INDEX)
                }
            }
        }
    }

    override fun onBindViewListener() {

    }

    override fun onBindViewValue() {
        (requireParentFragment() as AccessFragment).apply {
            next.setOnClickListener {
                //这里去请求服务器创建账号
                if (TextUtils.isEmpty(tUsername) || TextUtils.isEmpty(tAddress) || TextUtils.isEmpty(
                        tName
                    )
                ) {
//                    "数据错误，请重新扫描".shortToast()
//                    return@setOnClickListener
                }
                mAccessViewModel.registerAccess(
                    tUsername,
                    tAddress,
                    tName
                )
            }

            last.setOnClickListener {
                this.switchPage(AccessFragment.SUB_INFO_INDEX)
            }
        }
    }

    override fun onFindView(rootView: View?) {

    }

    override fun onLayoutId(): Int {
        return R.layout.fragment_id_access
    }

    override fun onPageExit() {

    }

}